package database

import java.sql.Connection
import java.sql.DriverManager

object DBConnMgr {
    private const val HANTANG_URL =
        "jdbc:mysql://127.0.0.1:3306/drift_bottle?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"
    private const val LOCAL_USER = "Yanhe"
    private const val LOCAL_PASSWORD = "Yanhe0711"
    private var conn: Connection = DriverManager.getConnection(HANTANG_URL, LOCAL_USER, LOCAL_PASSWORD)

    fun getConn(): Connection {
        if (conn.isClosed || !conn.isValid(1000)) {
            conn.close()
            conn = DriverManager.getConnection(HANTANG_URL, LOCAL_USER, LOCAL_PASSWORD)
        }
        return conn
    }
}